package cn.tedu.service.petcommodity.Impl;


import cn.tedu.commons.response.JsonPage;
import cn.tedu.mapper.petdetails.PetDetailsMapper;
import cn.tedu.pojo.pet.dto.PetDetailsDTO;
import cn.tedu.pojo.pet.entity.PetCommodity;
import cn.tedu.pojo.pet.entity.PetDetails;
import cn.tedu.pojo.pet.vo.PetDetailsVO;
import cn.tedu.service.petcommodity.IPetDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class PetDetailsImpl implements IPetDetailsService {

    @Autowired(required = false)
    private PetDetailsMapper petDetailsMapper;

    @Override
    public void addDetails(PetDetailsDTO petDetailsDTO) {
        PetCommodity commodityId=petDetailsMapper.getById(petDetailsDTO.getCommodityId());
        if (commodityId==null){
            System.out.println("添加详情失败，指定宠物商品id不存在");
            throw new RuntimeException();
        }
        PetDetails petDetails=new PetDetails();
        petDetails.setCommodityId(petDetailsDTO.getCommodityId());
        petDetails.setAge(petDetailsDTO.getAge());
        System.out.println("age:"+petDetailsDTO.getAge());
        petDetails.setDescription(petDetailsDTO.getDescription());
        petDetails.setPicture(petDetailsDTO.getPicture());
        System.out.println("picture:"+petDetailsDTO.getPicture());
        petDetails.setPrice(petDetailsDTO.getPrice());
        petDetails.setGander(petDetailsDTO.getGander());
        petDetails.setIsVaccination(petDetailsDTO.getIsVaccination());
        petDetails.setIsNeuter(petDetailsDTO.getIsNeuter());
        petDetails.setIsDelete(petDetailsDTO.getIsDelete());
        LocalDateTime now=LocalDateTime.now();
        petDetails.setGmtCreate(now);
        petDetails.setGmtModified(now);
        petDetailsMapper.addDetails(petDetails);
        System.out.println("详情添加成功");
    }

    @Override
    public void updateDetails(PetDetailsDTO petDetailsDTO) {
        System.out.println(petDetailsDTO.getId());
        PetDetails detailsExist=petDetailsMapper.getDetailsById(petDetailsDTO.getId());
        if (detailsExist==null){
            System.out.println("修改详情失败，所选宠物详情id不存在");
            throw new RuntimeException();
        }
        PetDetails petDetails=new PetDetails();
        petDetails.setId(petDetailsDTO.getId());
        petDetails.setCommodityId(petDetailsDTO.getCommodityId());
        petDetails.setAge(petDetailsDTO.getAge());
        petDetails.setDescription(petDetailsDTO.getDescription());
        petDetails.setPicture(petDetailsDTO.getPicture());
        System.out.println(petDetailsDTO.getAge());
        petDetails.setPrice(petDetailsDTO.getPrice());
        petDetails.setGander(petDetailsDTO.getGander());
        petDetails.setIsVaccination(petDetailsDTO.getIsVaccination());
        petDetails.setIsNeuter(petDetailsDTO.getIsNeuter());
        petDetails.setIsDelete(petDetails.getIsDelete());
        LocalDateTime now=LocalDateTime.now();
        petDetails.setGmtModified(now);
        System.out.println(petDetails);
        petDetailsMapper.updatePetDetails(petDetails);
        System.out.println("修改详情成功");


    }

    @Override
    public void updateIsDelete(Long id, Integer isDelete) {
        System.out.println("传入的id为："+id+"传入的数据为："+isDelete);
        petDetailsMapper.updateIsDelete(id,isDelete);
        System.out.println("详情启用情况修改成功");
    }


}
